GitHub Actionsの1つのJobでPythonのコードを整える
これまで:lint-checkジョブ、lintジョブで実装した
lint-checkが失敗するとlintジョブが動く
課題:lintジョブが正常終了しても、lint-checkが失敗しているので、ワークフローが失敗状態になってしまう
you can use black --check . as a condition
https://github.com/actions/toolkit/issues/908#issuecomment-919609272
code:shell
black --check . || black --fast .
…
git push
||(or operator)
black --check .が0以外の終了コードを返したとき(=blackでフォーマットする必要があるとき)に限り、black --fast .を実行
black --check .の終了コードが0のときは、blackでのフォーマットは実施されない
ref: https://missing.csail.mit.edu/2020/shell-tools/ from MIT コンピュータサイエンスの準備講座
||以降のgitコマンドはcleanな状態でcommitしたり、remoteに先行したコミットがないときにpushしたりしても、終了コードは0
あっても無害なので残すことにした(分岐を作るのは後回し)
GitHub Actionsでコミットしてpushするための設定
結論:actions/checkout@v2の恩恵にあずかっている
Version 2 of checkout resolves the detached HEAD state issue and simplifies pushing to origin.
ref: https://stackoverflow.com/a/58393457
secretsの設定が不要(${{ secrets.GITHUB_TOKEN }}が登場しない)
GitHub Actionsの secrets.GITHUB_TOKEN
ここまでにデバッグした雰囲気からactions/checkout@v2はActionをトリガーしたコミットに戻る?
https://github.com/ftnext/python-actions-example/issues/3#issuecomment-1003492992
たしかジョブが分かれていたケース
GITHUB_SHAが関係あるのかも
The commit SHA that triggered the workflow.
実際はgit pullがAlready up to date.なので杞憂だった(step間だから?)
Gitの設定
参考:GitHub Actions でワークフロー中に発生した差分を Push する
GITHUB_ACTOR
https://docs.github.com/ja/actions/learn-github-actions/environment-variables#default-environment-variables
The name of the person or app that initiated the workflow. For example, octocat.
これを指定することで自身としてコミットが作られる
1つのジョブで実行中のすべてのステップは同じランナー上で実行されるので、ステップ間でファイルシステムを通じて情報をやりとりすることができます。(Software Design (ソフトウェアデザイン) 2020年10月号 (Kindle の位置No.1934-1935) 第2特集がGitHub Actions)
git configでファイルに書き込むので、そのステップ一度で済む認識(DRYにできた)
今回は採用しなかったが参考になりそう
GITHUB_REPOSITORY
The owner and repository name. For example, octocat/Hello-World.
(actions/checkout@v2を使わない場合はget remote set-urlに必要になりそう)
GITHUB_HEAD_REF
Only set for pull request events. The name of the head branch.
今回はpull_requestイベントは考えていない
GitHub Actions で変更があるときだけ git commit & push する
回答元の質問 https://stackoverflow.com/q/57921401 、jqコマンドで GITHUB_EVENT_PATHのJSONをパースしている!
The path of the file with the complete webhook event payload. For example, /github/workflow/event.json.
この質問の回答 https://stackoverflow.com/a/58393457 で案内されている:GitHub Actions: How to Automate Code Formatting in Pull Requests